package kr.co.nextree.android.sample.activity;

import java.util.Calendar;

import kr.co.nextree.android.sample.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class DialogSamples extends Activity {
	private Button mBtnAlertDailog;
	private Button mBtnProgressDialog;
	private Button mBtnDatePickerDialog;
	private Button mBtnTimePickerDialog;
	private Button mBtnDialogActivity;
	private TextView mTextView;
	
	private int year;
	private int month;
	private int day;
	private int hour;
	private int minute;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dailog_samples);
		
		Calendar cal = Calendar.getInstance();
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH);
		day = cal.get(Calendar.DAY_OF_MONTH);
		hour = cal.get(Calendar.HOUR_OF_DAY);
		minute = cal.get(Calendar.MINUTE);
		
		
		mTextView = (TextView)findViewById(R.id.displayText);
		
		mBtnAlertDailog = (Button)findViewById(R.id.alertDialog);
		mBtnAlertDailog.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				/*AlertDialog.Builder ad = new AlertDialog.Builder(v.getContext());
				ad.setTitle("AlertDialog");
				ad.setMessage("AlertDialog 입니다.");
				ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				}).show();*/
				
				showDialog(ALERT_DIALOG);
				
			}
		});
		
		mBtnProgressDialog = (Button)findViewById(R.id.progressDialog);
		mBtnProgressDialog.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				/*ProgressDialog pd = new ProgressDialog(v.getContext());
				pd.setMessage("진행중입니다.");
				pd.setCancelable(true);
				pd.show();*/
				showDialog(PROGRESS_DIALOG);
				
				Thread thread = new Thread(new Runnable() {
					
					@Override
					public void run() {
						try {
							Thread.sleep(5000);
							dismissDialog(PROGRESS_DIALOG);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				});
				
				thread.start();				
			}
		});
		
		mBtnDatePickerDialog = (Button)findViewById(R.id.datePickerDialog);
		mBtnDatePickerDialog.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//new DatePickerDialog(v.getContext(), dp, 2010, 10, 12).show();
				showDialog(DATEPICKER_DIALOG);
				
			}
		});
		
		mBtnTimePickerDialog = (Button)findViewById(R.id.timePickerDialog);
		mBtnTimePickerDialog.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//new TimePickerDialog(v.getContext(), tp, 10, 30, false).show();
				showDialog(TIMEPICKER_DIALOG);
				
			}
		});
		
		mBtnDialogActivity = (Button)findViewById(R.id.activityDialog);
		mBtnDialogActivity.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(v.getContext(), DialogActivity.class));
			}
		});
		
	}
	
	DatePickerDialog.OnDateSetListener dp = new DatePickerDialog.OnDateSetListener() {
	
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			mTextView.setText(year + "년" + monthOfYear + "월" + dayOfMonth + "일");
		}
	};
	
	TimePickerDialog.OnTimeSetListener tp = new TimePickerDialog.OnTimeSetListener() {
		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mTextView.setText(hourOfDay + "시" + minute + "분");
		}
	};
	
	private static final int ALERT_DIALOG = 0;
	private static final int PROGRESS_DIALOG = 1;
	private static final int DATEPICKER_DIALOG = 2;
	private static final int TIMEPICKER_DIALOG = 3;

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ALERT_DIALOG:
			AlertDialog.Builder ad = new AlertDialog.Builder(this);
			ad.setTitle("AlertDialog");
			ad.setMessage("AlertDialog 입니다.");
			ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			});
			return ad.create();
		case PROGRESS_DIALOG:
			ProgressDialog pd = new ProgressDialog(this);
			pd.setMessage("진행중입니다.");
			pd.setCancelable(true);
			return pd;
		case DATEPICKER_DIALOG:
			return new DatePickerDialog(this, dp, year, month, day);
		case TIMEPICKER_DIALOG:
			return new TimePickerDialog(this, tp, hour, minute, false);
		}
		return super.onCreateDialog(id);
	}
	
	View.OnClickListener mClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.alertDialog:
				Toast.makeText(v.getContext(), "AlertDialog", Toast.LENGTH_SHORT).show();
				break;
			case R.id.progressDialog:
				Toast.makeText(v.getContext(), "ProgressDialog", Toast.LENGTH_SHORT).show();
				break;
			case R.id.datePickerDialog:
				Toast.makeText(v.getContext(), "DatePickerDialog", Toast.LENGTH_SHORT).show();
				break;
			case R.id.timePickerDialog:
				Toast.makeText(v.getContext(), "TimePickerDialog", Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}
		}
	};

}
